package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.config.LoginInterceptor;
import com.prd.mapper.EntContactForeignCheckMapper;
import com.prd.model.EntBudgetCheck;
import com.prd.model.EntContactForeignCheck;
import com.prd.model.LoginUser;
import com.prd.service.EntContactForeignCheckService;
import com.prd.util.UserInfoUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EntContactForeignCheckServiceImpl extends ServiceImpl<EntContactForeignCheckMapper, EntContactForeignCheck> implements EntContactForeignCheckService {

    @Autowired
    private EntContactForeignCheckMapper entContactForeignCheckMapper;

    @Override
    public int insertInfo(EntContactForeignCheck entContactForeignCheck) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        LambdaQueryWrapper<EntContactForeignCheck> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EntContactForeignCheck::getForeignId, entContactForeignCheck.getForeignId());
        queryWrapper.eq(EntContactForeignCheck::getType, entContactForeignCheck.getType());
        queryWrapper.eq(EntContactForeignCheck::getCreateId, loginUser.getId());
        EntContactForeignCheck check = entContactForeignCheckMapper.selectOne(queryWrapper);
        if (check != null) {
            throw new RuntimeException("该记录已经存在！");
        }
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), true), entContactForeignCheck);
        return entContactForeignCheckMapper.insert(entContactForeignCheck);
    }

    @Override
    public int updateInfo(EntContactForeignCheck entContactForeignCheck) {
        LoginUser loginUser = LoginInterceptor.threadLocal.get();
        BeanUtils.copyProperties(UserInfoUtil.recordInfo(loginUser.getId(), loginUser.getName(), false), entContactForeignCheck);
        return entContactForeignCheckMapper.updateById(entContactForeignCheck);
    }
}
